package com.webreport.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.webreport.dao.UserDao;
import com.webreport.exception.DaoException;
import com.webreport.exception.LoginException;
import com.webreport.exception.ManagerException;
import com.webreport.model.db.User;
import com.webreport.service.UserManager;
import com.webreport.utils.EncryptUtil;
@Service("userManager")
public class UserManagerImpl extends GenericManagerImpl<User,String> implements UserManager{
	UserDao dao;
	
	@Autowired
	public UserManagerImpl(UserDao dao) {
		super(dao);
		this.dao = dao;
	}

	public User login(String username, String password) throws ManagerException, LoginException {
		User user = null;
		try {
			user = this.dao.queryByUsername(username);
		} catch (DaoException e) {
			log.error("查询用户失败" + e.getMessage());
			throw new ManagerException("查询用户失败");
		}
		if (user == null)
			throw new LoginException("用户名或密码错误！");
		if (!EncryptUtil.match(password,user.getPassword())){
			throw new LoginException("用户名或密码错误！");
		}
		if (!user.isEnabled()){
			throw new LoginException("用户不可用,请联系系统管理员！");
		}
		return user;
	}
	
	
}
